﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Runtime.Serialization; // This is for the DataMemberAttribute so we can order our keys
using System.Web.Mvc; 

namespace BlogLite.Models.Database
{
    /// <summary>
    /// This class represents the concept of a Post
    /// </summary>
    public class Post
    {
        [Key]
        public int PostId { get; set; }     

        [Required(ErrorMessage="Post date is required")]
        [DataType(DataType.Date)]
        public DateTime PostDate { get; set; }
        
        [Required(ErrorMessage="Post title is required")]
        [AllowHtml]        
        public string Subject { get; set; }

        [Required(ErrorMessage="Post content is required")]
        [AllowHtml]
        public string Content { get; set; }

        public DateTime LastModified { get; set; }

        // Navigation Properties
        public virtual User User { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
        public virtual ICollection<Tag> Tags { get; set; }
    }

    /// <summary>
    /// This class represents the concept of a Comment    
    /// </summary>
    public class Comment
    {
        [Key]
        public int CommentId { get; set; }

        public int PostId { get; set; }

        [Required]
        public string Name { get; set; }
        
        public string Email { get; set; }
        
        public string GravatarHash { get; set; }
        
        public string OpenId { get; set; }
        
        public string Website { get; set; }

        public DateTime CommentDate { get; set; }

        [Required]
        public string CommentContent { get; set; }

        public virtual Post Post { get; set; }
    }   

    /// <summary>
    /// This class represents the concept of a Tag
    /// This will be used as both a lookup table as well as
    /// a place where we can add new distinct tags
    /// </summary>
    public class Tag
    {
        [Key]
        public int TagId { get; set; }

        [Required]
        public string TagName { get; set; }

        public virtual ICollection<Post> Posts { get; set; }
    }

    /// <summary>
    /// This class represents the concept of a User
    /// </summary>
    public class User
    {
        [Key]
        public int UserId { get; set; }

        [Required]
        public string DisplayName { get; set; }

        public virtual aspnet_Users aspnet_User { get; set; }
    }

    #region Membership

        public class aspnet_Users
        {     
            [Key]
            public Guid PKID { get; set; }

            [Required]
            [StringLength(255)]
			public string Username { get; set; }

            [Required]
            [StringLength(100)]
            public string ApplicationName { get; set; }

            [Required]
            [StringLength(100)]
			public string Email { get; set; }

            [StringLength(255)]
			public string Comment { get; set; }

            [Required]
            [StringLength(128)]
			public string Password { get; set; }

            [StringLength(255)]
			public string PasswordQuestion { get; set; }
			
            [StringLength(255)]
            public string PasswordAnswer { get; set; }
			public bool? IsApproved  { get; set; }
			public DateTime? LastActivityDate { get; set; }
            public DateTime? LastLoginDate { get; set; }
            public DateTime? LastPasswordChangedDate { get; set; }
            public DateTime? CreationDate { get; set; }
            public bool? IsOnLine { get; set; }
            public bool? IsLockedOut { get; set; }
            public DateTime LastLockedOutDate { get; set; }
            public int? FailedPasswordAttemptCount { get; set; }
            public DateTime? FailedPasswordAttemptWindowStart { get; set; }
            public int? FailedPasswordAnswerAttemptCount { get; set; }
			public DateTime? FailedPasswordAnswerAttemptWindowStart { get; set; }
        }

    #endregion   
    
    /// <summary>
    /// This will become the database context for our blog    
    /// </summary>
    public class BlogLiteContext : DbContext
    {
        // Setup all of our DBSets in our database
        // These will appear to be Entities when we ues LINQ to Entites        
        public DbSet<Post> Posts { get; set; }
        public DbSet<Comment> Comments { get; set; }     
        public DbSet<Tag> Tags { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<aspnet_Users> MembershipUsers { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {              
            base.OnModelCreating(modelBuilder);
        }
    }
}